<template>
  <el-dialog
    title="订单操作"
    :visible.sync="visible"
    :close-on-click-modal="false"
    append-to-body
  >
    <el-form
      ref="main-form"
      status-icon
      :model="mainQuery"
      :rules="rules"
      label-width="120px"
      label-position="left"
      :inline="false"
      style="width: 70%"
    >
      <el-form-item v-if="!!data" label="商户号" size="mini" prop="">
        <span v-text="data.mchNo"></span>
      </el-form-item>

      <el-form-item v-if="!!data" label="事件订单号" size="mini" prop="">
        <span v-text="data.eventOrderNo"></span>
      </el-form-item>
      <el-form-item v-if="!!data" label="事件变动" size="mini" prop="">
        <span v-text="data.floatAmount"></span>
      </el-form-item>

      <el-form-item v-if="!!data" label="事件类型" size="mini" prop="">
        <span v-text="data.strType"></span>
      </el-form-item>

      <el-form-item v-if="!!data" label="事件状态" size="mini" prop="">
        <span v-text="data.strHandleStatus"></span>
      </el-form-item>

      <el-form-item
        v-if="!!data && currentRemarkVisible"
        label="备注"
        size="mini"
        prop=""
      >
        <span v-text="currentRemark"></span>
      </el-form-item>

      <el-form-item label="审核意见" size="mini" prop="remark">
        <el-input
          placeholder=""
          type="textarea"
          v-model="mainQuery.remark"
          clearable
        ></el-input>
      </el-form-item>
    </el-form>

    <div slot="footer" class="dialog-footer">
      <!-- <el-popover
          v-if="id != null"
          placement="top"
          width="160"
          v-model="popDeleteVisible">
          <p>确定删除吗？</p>
          <div style="text-align: right; margin: 0">
            <el-button size="mini" type="default" @click="popDeleteVisible = false">取消</el-button>
            <el-button type="danger" size="mini" @click="submitDel">确定</el-button>
          </div>
          <el-button type="danger" slot="reference"  size="small" style="float: left;" :loading="loading">删 除</el-button>
        </el-popover> -->

      <el-button @click="close" size="mini">取 消</el-button>

      <el-popover
        v-if="rejectVisible"
        placement="top"
        width="160"
        v-model="popRejectVisible"
      >
        <p>确定驳回审核吗?</p>
        <div style="text-align: right; margin: 0">
          <el-button
            size="mini"
            type="default"
            @click="popRejectVisible = false"
            >取消</el-button
          >
          <el-button
            type="danger"
            size="mini"
            @click="submit(false)"
            :loading="loading"
            >驳回</el-button
          >
        </div>
        <el-button
          slot="reference"
          size="mini"
          type="danger"
          :loading="loading"
          style="float: left"
          >驳回审核</el-button
        >
      </el-popover>

      <el-popover
        v-if="action == 'unfreeze'"
        placement="top"
        width="160"
        v-model="popConfirmVisible"
      >
        <p>确定解冻吗?</p>
        <div style="text-align: right; margin: 0">
          <el-button
            size="mini"
            type="default"
            @click="popConfirmVisible = false"
            >取消</el-button
          >
          <el-button
            type="success"
            size="mini"
            @click="submit(true)"
            :loading="loading"
            >解冻</el-button
          >
        </div>
        <el-button
          slot="reference"
          size="mini"
          type="success"
          :loading="loading"
          style="margin-left: 10px"
          >解冻</el-button
        >
      </el-popover>
      <el-popover
        v-else
        placement="top"
        width="160"
        v-model="popConfirmVisible"
      >
        <p>确定通过审核吗?</p>
        <div style="text-align: right; margin: 0">
          <el-button
            size="mini"
            type="default"
            @click="popConfirmVisible = false"
            >取消</el-button
          >
          <el-button
            type="primary"
            size="mini"
            @click="submit(true)"
            :loading="loading"
            >通过</el-button
          >
        </div>
        <el-button
          slot="reference"
          size="mini"
          type="primary"
          :loading="loading"
          style="margin-left: 10px"
          >通过审核</el-button
        >
      </el-popover>
    </div>
  </el-dialog>
</template>

<script>

import ApiMain from '@/api/settle.operatetrade';

export default {
  mixins: [
  ],
  props: [
  ],
  data() {
    return {

      visible: false,
      rejectVisible: false,
      currentRemarkVisible: false,
      popConfirmVisible: false,
      popRejectVisible: false,
      loading: false,

      mainQueryRaw: null,
      mainQuery: {
        remark: '',
      },

      rules: {
        remark: [
          { required: true, message: '请填写内容', trigger: 'blur' },
        ],
      },

      id: null,
      data: null,

      action: '',
      strType: '',
      currentRemark: '',

    }
  },
  mounted() {
    this.mainQueryRaw = Object.copy(this.mainQuery);
  },

  methods: {

    show(data, action) {

      this.reset();
      console.log('data',data)
      this.data = data;
      this.action = action;
      this.rejectVisible = true;
      this.currentRemarkVisible = true;

      switch (action) {
        case 'check':
          this.strType = '初审';
          this.currentRemark = this.data.createRemark;
          break;
        case 'recheck':
          this.strType = '复核';
          this.currentRemark = this.data.checkRemark;
          break;
        case 'money':
          this.strType = '出款';
          this.currentRemark = this.data.reCheckRemark;
          this.rejectVisible = false;
          break;
        case 'unfreeze':
          this.strType = '解冻';
          this.currentRemark = '';
          this.rejectVisible = false;
          this.currentRemarkVisible = false;
          break;
      }

      this.visible = true;
    },

    close() {
      this.visible = false;
      this.$emit('dialog-close');
    },

    reset() {
      this.mainQuery = Object.copy(this.mainQueryRaw);
      this.$refs['main-form'] && this.$refs['main-form'].resetFields();
      this.id = null;
      this.data = null;
    },

    verifyForm() {
      var bool = true;
      this.$refs['main-form'].validate((valid) => bool = valid);
      return bool;
    },

    async submit(isConfirm) {

      this.popConfirmVisible = false;
      this.popRejectVisible = false;

      var req = Object.copy(this.mainQuery);
      req.id = this.data.id;
      req.isConfirm = isConfirm;

      this.loading = true;

      var re = null;

      try {
        switch (this.action) {
          case 'check':
            re = await ApiMain.actionCheck(req);
            break;
          case 'recheck':
            re = await ApiMain.actionRecheck(req);
            break;
          case 'money':
            re = await ApiMain.actionMoney(req);
            break;
          case 'unfreeze':
            re = await ApiMain.actionUnfreeze(req);
            break;
        }
      } catch (err) { console.error(err) }


      console.log(re)

      this.$message({ message: '操作成功', type: 'success' });
      this.loading = false;

      this.close();
    },

  }
}
</script>

<style>
</style>
